package com.niit.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    int n =1;
    another fragment = new another();
    another2 fragment2 = new another2();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if(n == 1) {
                    transaction.replace(R.id.bottom_layout, fragment);
                    transaction.commit();
                    n =2;
                }
                else if (n == 2){
                    transaction.replace(R.id.bottom_layout, fragment2);
                    transaction.commit();
                    n = 0;
                }
                else if ( n== 0){
                    n = 1;
                }
            }
        });
    }
}
